<?php

namespace app\admin\controller\group_activity\config;

use app\BaseController;
use app\model\group_activity\GroupActivityTypeModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 类型列表
     * @return bool|string
     */
    public function TypeList(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页显示数量' => 'require',
            ]);
            $where = [];
            if (!empty($param['name'])) {
                $where[] = ['name', 'like', '%' . $param['name'] . '%'];
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $GroupActivityTypeModel = new GroupActivityTypeModel();
            $query = $GroupActivityTypeModel->where($where);
            $data = $query->with([
                'user'
            ])->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 类型添加
     * @return bool|string
     */
    public function TypeAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'name|名称' => 'require',
            ]);
            $GroupActivityTypeModel = new GroupActivityTypeModel();
            $param['unit_id'] = $this->userData['unit_id'];
            $param['uid'] = $this->userData['id'];
            $param['time'] = time();
            $GroupActivityTypeModel->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 类型修改
     * @return bool|string
     */
    public function TypeEdit(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require|integer|max:11',
                'name|名称' => 'require|string',
            ]);
            $GroupActivityTypeModel = new GroupActivityTypeModel();
            $info = $GroupActivityTypeModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 类型删除
     * @return bool|string
     */
    public function TypeDel(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require',
            ]);
            $GroupActivityTypeModel = new GroupActivityTypeModel();
            if (is_array($param['id'])) {
                $GroupActivityTypeModel->where('id', 'in', $param['id'])->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            } else {
                $info = $GroupActivityTypeModel->find($param['id']);
                if (!$info) throw new \Exception('参数错误！');
                $info->is_del = 1;
                $info->del_time = time();
                $info->save();
            }
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 修改类型状态
     * @return bool|string
     */
    public function TypeStatus(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require',
                'status|状态' => 'require',
            ]);
            $GroupActivityTypeModel = new GroupActivityTypeModel();
            if (is_array($param['id'])) {
                $GroupActivityTypeModel->where('id', 'in', $param['id'])->update([
                    'status' => $param['status']
                ]);
            } else {
                $info = $GroupActivityTypeModel->find($param['id']);
                if (!$info) throw new \Exception('参数错误！');
                $info->status = $param['status'];
                $info->save();
            }
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 获取类型信息
     * @return bool|string
     */
    public function TypeInfo(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'id|ID' => 'require',
            ]);
            $GroupActivityTypeModel = new GroupActivityTypeModel();
            $info = $GroupActivityTypeModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

}